package com.gin.kotlin.gf2gamecommunity.viewmodel

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.gin.kotlin.enums.ComplaintReason
import com.gin.kotlin.form.CommentForm
import com.gin.kotlin.form.CommentListForm
import com.gin.kotlin.form.ComplaintForm
import com.gin.kotlin.gf2gamecommunity.app.App
import com.gin.kotlin.gf2gamecommunity.base.livedata.CachedPagingData
import com.gin.kotlin.gf2gamecommunity.base.livedata.ParamCachedLiveData
import com.gin.kotlin.gf2gamecommunity.callback.AbstractNothingCallback
import com.gin.kotlin.gf2gamecommunity.pagingsource.CommentInfoPagingSource
import com.gin.kotlin.response.Res
import com.gin.kotlin.response.comment.CommentInfo

class CommentListViewModel : ViewModel() {
    val commentList = CachedPagingData<CommentInfo, CommentListForm>(scope = viewModelScope) { CommentInfoPagingSource(it) }

    /**
     *  单个楼层信息
     */
    val floorInfo = ParamCachedLiveData<Res<CommentInfo>, CommentInfo, CommentForm>(
        requestMethod = { api().floorInfo(it) },
        bodyTransform = { it.data },
    )


    /**
     *  删除回复
     * @param topicId 主题ID
     * @param commentId 回复ID
     * @param onSuccess 成功回调
     */
    fun remove(topicId: Int, commentId: Int, onSuccess: () -> Unit = {}) = api().remove(topicId, commentId).enqueue(object : AbstractNothingCallback() {
        override fun onSuccess(message: String) {
            App.toast(message)
            onSuccess()
        }
    })


    /**
     * 点赞回复
     * @param topicId 主题ID
     * @param commentId 回复ID
     * @param onSuccess 成功回调
     */
    fun like(topicId: Int, commentId: Int, onSuccess: () -> Unit = {}) = api().like(CommentForm(topicId, commentId)).enqueue(object : AbstractNothingCallback() {
        override fun onSuccess(message: String) {
            App.toast(message)
            onSuccess()
        }
    })

    /**
     * 点踩回复
     * @param topicId 主题ID
     * @param commentId 回复ID
     * @param onSuccess 成功回调
     */
    fun bad(topicId: Int, commentId: Int, onSuccess: () -> Unit = {}) = api().bad(CommentForm(topicId, commentId)).enqueue(object : AbstractNothingCallback() {
        override fun onSuccess(message: String) {
            App.toast(message)
            onSuccess()
        }
    })

    /**
     * 举报回复
     * @param topicId Int
     * @param reason ComplaintReason
     * @param onSuccess () -> Unit
     */
    fun complaint(topicId: Int, reason: ComplaintReason, commentId: Int, onSuccess: () -> Unit = {}) =
        api().complaint(ComplaintForm(topicId, reason, commentId)).enqueue(object : AbstractNothingCallback() {
            override fun onSuccess(message: String) {
                App.toast(message)
                onSuccess()
            }
        })


    private fun api() = App.INSTANCE.api.commentApi

    companion object {
        private val TAG = CommentListViewModel::class.java.simpleName
    }
}